{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# python序列结构的(列表、元组、集合、字典)复习:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'人生得意须尽欢'"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Libai = ['黄河之水天上来','奔流到海不复回','人生得意须尽欢','莫使金樽空对月']\n",
    "#正向索引\n",
    "Libai[2]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'莫使金樽空对月'"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#反向索引倒数第一个元素\n",
    "Libai[-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['奔流到海不复回', '人生得意须尽欢']"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#切片(Slicing)\n",
    "Libai[1:3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['黄河之水天上来', '奔流到海不复回', '人生得意须尽欢', '莫使金樽空对月', '风急天高猿啸哀', '无边落木萧萧下', '不尽长江滚滚来']"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Dufu = ['风急天高猿啸哀','无边落木萧萧下','不尽长江滚滚来']\n",
    "#序列相加(Adding),序列相加必须是同一类序列结构\n",
    "Libai+Dufu"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['风急天高猿啸哀', '无边落木萧萧下', '不尽长江滚滚来', '风急天高猿啸哀', '无边落木萧萧下', '不尽长江滚滚来', '风急天高猿啸哀', '无边落木萧萧下', '不尽长江滚滚来']\n"
     ]
    }
   ],
   "source": [
    "#乘法(Multiplying)\n",
    "Poetry = Dufu*3\n",
    "print(Poetry)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "False\n"
     ]
    }
   ],
   "source": [
    "#检查某个元素是否是序列成员\n",
    "print('渚轻沙白鸟飞回' in Dufu)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]\n",
      "0\n",
      "10\n"
     ]
    }
   ],
   "source": [
    "#计算序列长度、最大值、最小值\n",
    "Number = list(range(10))\n",
    "print(Number)\n",
    "print(min(Number))\n",
    "print(len(Number))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 一、列表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'Number' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-18-ceac0533fbf2>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[1;31m#删除列表\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      2\u001b[0m \u001b[1;32mdel\u001b[0m \u001b[0mNumber\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 3\u001b[1;33m \u001b[0mNumber\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m: name 'Number' is not defined"
     ]
    }
   ],
   "source": [
    "#删除列表\n",
    "del Number\n",
    "Number"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "黄河之水天上来\n",
      "奔流到海不复回\n",
      "人生得意须尽欢\n",
      "莫使金樽空对月\n"
     ]
    }
   ],
   "source": [
    "# 遍历列表\n",
    "for x in Libai:\n",
    "    print(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 风急天高猿啸哀\n",
      "1 无边落木萧萧下\n",
      "2 不尽长江滚滚来\n"
     ]
    }
   ],
   "source": [
    "# 使用enumerate()函数实现输出index和value\n",
    "for index,value in enumerate(Dufu):\n",
    "    print(index,value)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['风急天高猿啸哀', '无边落木萧萧下', '不尽长江滚滚来', '渚轻沙白鸟飞回', ['渚轻沙白鸟飞回', '登高']]"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#添加、修改、删除列表元素\n",
    "Dufu.append(['渚轻沙白鸟飞回','登高'])\n",
    "Dufu"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "append() takes exactly one argument (2 given)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-24-ae27ebfc764c>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[1;31m#\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[0mDufu\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mappend\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'du'\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;34m'fu'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m: append() takes exactly one argument (2 given)"
     ]
    }
   ],
   "source": [
    "#\n",
    "Dufu.append('du','fu')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "> 显然append只能够追加一个元素或者是对象，要扩展多个元素只能用extend方法"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Dufu.extend(['杜工部','登高'])\n",
    "Dufu"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['风急天高猿啸哀', '无边落木萧萧下', '不尽长江滚滚来', '渚轻沙白鸟飞回', '登高']"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#修改元素\n",
    "Dufu[4] = '登高'\n",
    "Dufu"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['风急天高猿啸哀', '无边落木萧萧下', '不尽长江滚滚来', '渚轻沙白鸟飞回']"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#删除元素\n",
    "del Dufu[-1]\n",
    "Dufu"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['风急天高猿啸哀', '无边落木萧萧下', '不尽长江滚滚来', '渚轻沙白鸟飞回', '杜工部', '登高', '杜工部', '登高']"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#对列表进行统计计算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['风急天高猿啸哀', '无边落木萧萧下', '不尽长江滚滚来', '渚轻沙白鸟飞回', '杜工部', '登高', '杜工部', '登高', '风急天高猿啸哀', '无边落木萧萧下', '不尽长江滚滚来', '渚轻沙白鸟飞回', '杜工部', '登高', '杜工部', '登高', '风急天高猿啸哀', '无边落木萧萧下', '不尽长江滚滚来', '渚轻沙白鸟飞回', '杜工部', '登高', '杜工部', '登高']\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "6"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "TDufu = Dufu*3\n",
    "print(TDufu)\n",
    "TDufu.count('登高')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['风急天高猿啸哀', '无边落木萧萧下', '不尽长江滚滚来', '渚轻沙白鸟飞回', '杜工部', '杜工部', '风急天高猿啸哀', '无边落木萧萧下', '不尽长江滚滚来', '渚轻沙白鸟飞回', '杜工部', '杜工部', '风急天高猿啸哀', '无边落木萧萧下', '不尽长江滚滚来', '渚轻沙白鸟飞回', '杜工部', '杜工部']\n"
     ]
    }
   ],
   "source": [
    "#使用remove方法删除元素\n",
    "while TDufu.count('登高'):\n",
    "    TDufu.remove('登高')\n",
    "print(TDufu)    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n",
      "['风急天高猿啸哀', '无边落木萧萧下', '不尽长江滚滚来', '渚轻沙白鸟飞回', '风急天高猿啸哀', '无边落木萧萧下', '不尽长江滚滚来', '渚轻沙白鸟飞回', '风急天高猿啸哀', '无边落木萧萧下', '不尽长江滚滚来', '渚轻沙白鸟飞回', '杜工部']\n",
      "1\n"
     ]
    }
   ],
   "source": [
    "print(TDufu.count('杜工部'))\n",
    "if TDufu.count('杜工部')>0:\n",
    "    TDufu.remove('杜工部')\n",
    "print(TDufu)\n",
    "print(TDufu.count('杜工部'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 获取元素首次出现下标\n",
    "TDufu.index('无边落木萧萧下')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[100, 86, 13, 29, 51, 24, 21, 42, 48, 47]"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#对列表进行排序\n",
    "import random\n",
    "NumList = [random.randint(10,100) for i in range(10)]\n",
    "NumList"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* Listname.sort(key=None,reverse=False)\n",
    "> 参数说明如下:\n",
    "key:表示列表中指定一个比较键(如:key=str.lower)表示不区分大小写\n",
    "reverse:表示降序或者是升序排列\n",
    "* sorted(iterable,key=None,reverse=False)\n",
    "> 参数说明:\n",
    "iterable:表示可迭代的对象\n",
    "* sort和sorted的差别\n",
    "> 首先参数上面有差别很显然，sort方法是列表对象的内置方法只面向列表而sorted函数是内置函数面向的所有对象\n",
    "其次sort方法不能直接对中文进行处理，而sorted函数可以直接处理，另外两者对字符串进行排序都是先按照首字母在其他字母"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "升序排列: [13, 21, 24, 29, 42, 47, 48, 51, 86, 100]\n",
      "降序排列: [100, 86, 51, 48, 47, 42, 29, 24, 21, 13]\n"
     ]
    }
   ],
   "source": [
    "#使用sort方法\n",
    "NumList.sort()\n",
    "print('升序排列:',NumList)\n",
    "NumList.sort(reverse = True)\n",
    "print('降序排列:',NumList)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 列表推导式:\n",
    " `list = [Expression for Variable in object]`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[82, 100, 61, 59, 100, 86, 68, 43, 21, 80]"
      ]
     },
     "execution_count": 63,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import random\n",
    "Listrandom = [random.randint(10,100) for x in range(10)]\n",
    "Listrandom"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[0.0, 0.5, 1.0, 1.5, 2.0, 2.5, 3.0, 3.5, 4.0, 4.5]"
      ]
     },
     "execution_count": 76,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "price = [(i/2) for i in range(10)]\n",
    "price"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 二、元组tuple\n",
    "> 元组与其他序列结构不同之处在于不能够修改元组中的元素，元组中的元素与列表一样可以使任意类型访问元组中的元素也与列表的方式一样，支持索引切片合并"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 元组推导式:`(Expression for i in object)`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 89,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<generator object <genexpr> at 0x00000271FF127AF0>\n",
      "(778, 973, 133, 786, 500, 903, 738, 633, 614, 263)\n",
      "[]\n"
     ]
    }
   ],
   "source": [
    "import random\n",
    "Rtuple = (random.randint(100,1000) for i in range(10))\n",
    "print(Rtuple)\n",
    "print(tuple(Rtuple))\n",
    "print(list(Rtuple))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    ">可以看到元组推到式生成的不是一个元组或者列表而是一个生成器对象,可以由其生成列表或元组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 94,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[315, 1000, 385, 219, 898, 421, 138, 731, 884, 448]\n",
      "()\n"
     ]
    }
   ],
   "source": [
    "#生成元组\n",
    "Rltuple = (random.randint(100,1000) for i in range(10))\n",
    "listtuple = list(Rltuple)\n",
    "print(listtuple)\n",
    "print(tuple(Rltuple))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    ">由上面还可以看到生成器对象是一次性的，使用完就空空如也了"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 三、字典(dictionary)\n",
    "> 顾名思义，字典这个序列就够相当于我们的新华字典，可以通过查找拼音找到汉字，而python中的字典是由一些列的键-值对来构成的\n",
    "有几点需要说明的是：\n",
    "* 字典是通过键而不是索引来读取\n",
    "* 字典是任意对象的无序集合\n",
    "* 字典可变可任意嵌套\n",
    "* 字典中的键必须唯一\n",
    "* 字典的键不可变(为的是防止冲突)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 字典的创建:\n",
    "1.直接创建语法格式:`dictionary = {'key1':'value1',...}`\n",
    ">参数说明:\n",
    "key表示的键可以是字符串、数字、元组、汉字等，但不能用列表，且必须唯一\n",
    "value为键值可以是任意数据类型且可以不唯一\n",
    "\n",
    "2.通过指定键值对创键:`dictionary = dict(key1=value,....)`\n",
    "\n",
    "3.通过zip函数构建一一映射创建:\n",
    "`dictionary = dict(zip(object1,object2))`\n",
    ">参数说明:\n",
    "object表示将两个元组或者是列表构成一个一一映射形成键值对，长度不同则由短板效应决定"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 112,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'one': 1, 'two': 2}\n",
      "{1: 1}\n",
      "{1: 1}\n"
     ]
    },
    {
     "ename": "TypeError",
     "evalue": "unhashable type: 'dict'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-112-a83673e65c42>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m      6\u001b[0m \u001b[0mNumdict4\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;33m{\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m}\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      7\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mNumdict4\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 8\u001b[1;33m \u001b[0mNumdict5\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;33m{\u001b[0m\u001b[1;33m{\u001b[0m\u001b[1;33m}\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m}\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      9\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mNumdict5\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     10\u001b[0m \u001b[0mNumdict3\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;33m{\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m}\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mTypeError\u001b[0m: unhashable type: 'dict'"
     ]
    }
   ],
   "source": [
    "# 直接创建\n",
    "Numdict = {'one':1,'two':2}\n",
    "print(Numdict)\n",
    "Numdict2 = {(1):1}\n",
    "print(Numdict2)\n",
    "Numdict4 = {1:1}\n",
    "print(Numdict4)\n",
    "Numdict5 = {{}:1}\n",
    "print(Numdict5)\n",
    "Numdict3 = {[1]:1}\n",
    "print(Numdict3)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    ">由上面可知字典的键不能用列表不能用字典但可以用元组、数字、字符串"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 113,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'陆小凤': '灵犀一指', '西门吹雪': '天外飞仙'}\n"
     ]
    }
   ],
   "source": [
    "# 通过键值对创建\n",
    "Gulongdict = dict(陆小凤 = '灵犀一指',西门吹雪 = '天外飞仙')\n",
    "print(Gulongdict)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 115,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'李寻欢': '林诗音', '萧十一郎': '风四娘', '楚留香': '苏蓉蓉'}\n"
     ]
    }
   ],
   "source": [
    "# 通过zip函数一一映射\n",
    "listone = ['李寻欢','楚留香','萧十一郎']\n",
    "listtwo = ['林诗音','苏蓉蓉','风四娘','林仙儿']\n",
    "Reflectdict = dict(zip(listone,listtwo))\n",
    "print(Reflectdict)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 117,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "创建一个值为空的字典 {'李寻欢': None, '萧十一郎': None, '楚留香': None}\n"
     ]
    }
   ],
   "source": [
    "# 通过dict对象的fromkeys创建字典\n",
    "dictionary = dict.fromkeys(listone)\n",
    "print('创建一个值为空的字典',dictionary)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 120,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'listone': ['林诗音', '苏蓉蓉', '风四娘', '林仙儿']}\n"
     ]
    }
   ],
   "source": [
    "# 将元组作为键列表作为值\n",
    "listone = ('李寻欢','楚留香','萧十一郎')\n",
    "listtwo = ['林诗音','苏蓉蓉','风四娘','林仙儿']\n",
    "diction = dict(listone = listtwo)\n",
    "print(diction)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    ">可以看到用键值对来构建字典的时候其键值不能直接引用变量的数据，而是用的变量名"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 121,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{('李寻欢', '楚留香', '萧十一郎'): ['林诗音', '苏蓉蓉', '风四娘', '林仙儿']}\n"
     ]
    }
   ],
   "source": [
    "diction = {listone:listtwo}\n",
    "print(diction)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 122,
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "unhashable type: 'list'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-122-8d4b0dc83e7c>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mdiction\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;33m{\u001b[0m\u001b[0mlisttwo\u001b[0m\u001b[1;33m:\u001b[0m\u001b[0mlistone\u001b[0m\u001b[1;33m}\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m: unhashable type: 'list'"
     ]
    }
   ],
   "source": [
    "diction = {listtwo:listone}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    ">出错！在一次验证列表是不能作为字典的键的"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 123,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'李寻欢': '林诗音', '萧十一郎': '风四娘', '楚留香': '苏蓉蓉'}\n"
     ]
    }
   ],
   "source": [
    "# 通过键值对来访问字典\n",
    "print(Reflectdict)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 125,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "李寻欢喜欢的人是: 林诗音\n"
     ]
    }
   ],
   "source": [
    "print('李寻欢喜欢的人是:',Reflectdict['李寻欢'] if '李寻欢'  in Reflectdict else '我的字典里么有这个妹妹')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 127,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'苏蓉蓉'"
      ]
     },
     "execution_count": 127,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'''\n",
    "通过dict内置方法get来获取value这种方法有助于当键不存在是通过键索引值是有异常抛出，\n",
    " get函数的好处就在于没有键的时候会返回None不会抛出异常中断程序\n",
    "'''\n",
    "Reflectdict.get('楚留香')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 130,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "None\n"
     ]
    }
   ],
   "source": [
    "print(Reflectdict.get('特朗普'))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 遍历字典可以通过字典对象的item方法获取键值对:`dictionary.items()`\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 131,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "('李寻欢', '林诗音')\n",
      "('萧十一郎', '风四娘')\n",
      "('楚留香', '苏蓉蓉')\n"
     ]
    }
   ],
   "source": [
    "for item in Reflectdict.items():\n",
    "    print(item)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 133,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dict_items([('李寻欢', '林诗音'), ('萧十一郎', '风四娘'), ('楚留香', '苏蓉蓉')])"
      ]
     },
     "execution_count": 133,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Reflectdict.items()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    ">可以看到items时间字典中的键值对做成了元组然后在塞入一个列表中上面遍历显示的是元组就不奇怪了"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 137,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "李寻欢 喜欢的人是 林诗音\n",
      "萧十一郎 喜欢的人是 风四娘\n",
      "楚留香 喜欢的人是 苏蓉蓉\n"
     ]
    }
   ],
   "source": [
    "#把键和值分开拿出来\n",
    "for key,value in Reflectdict.items():\n",
    "    print(key,'喜欢的人是',value)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 138,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dict_keys(['李寻欢', '萧十一郎', '楚留香'])"
      ]
     },
     "execution_count": 138,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#单独拿出键\n",
    "Reflectdict.keys()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 139,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dict_values(['林诗音', '风四娘', '苏蓉蓉'])"
      ]
     },
     "execution_count": 139,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 单独拿出值\n",
    "Reflectdict.values()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 增加、修改和删除字典:\n",
    "* 增加一个键值对:`dictionary[key] = value`\n",
    "* 删除一个键值对:`del dictionar[key]`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 141,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'李寻欢': '林诗音', '楚留香': '苏蓉蓉', '萧十一郎': '风四娘'}"
      ]
     },
     "execution_count": 141,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dictionary = dict((('楚留香','苏蓉蓉'),(\"萧十一郎\",\"风四娘\"),(\"李寻欢\",\"林诗音\")))\n",
    "dictionary"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 142,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'李寻欢': '林诗音', '萧十一郎': '风四娘', '楚留香': '苏蓉蓉', '阿飞': '林仙儿'}\n"
     ]
    }
   ],
   "source": [
    "#增加一个键值对\n",
    "dictionary['阿飞'] = '林仙儿'\n",
    "print(dictionary)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 143,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'李寻欢': '林诗音', '萧十一郎': '风四娘', '楚留香': '苏蓉蓉'}\n"
     ]
    }
   ],
   "source": [
    "# 删除一个键值对\n",
    "del dictionary['阿飞']\n",
    "print(dictionary)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 144,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'李寻欢': '林诗音', '萧十一郎': '风四娘', '楚留香': '苏蓉蓉', '阿飞': '林仙儿'}\n",
      "{'李寻欢': '林诗音', '萧十一郎': '风四娘', '楚留香': '苏蓉蓉'}\n"
     ]
    }
   ],
   "source": [
    "#两步合二为一\n",
    "dictionary['阿飞'] = '林仙儿'\n",
    "print(dictionary)\n",
    "if '阿飞' in dictionary:\n",
    "    del dictionary['阿飞']\n",
    "else:\n",
    "    pass\n",
    "print(dictionary)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 145,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{0: 12, 1: 15, 2: 69, 3: 78, 4: 43, 5: 17, 6: 22, 7: 92, 8: 69, 9: 11}\n"
     ]
    }
   ],
   "source": [
    "# 字典推导式:\n",
    "import random\n",
    "Xinhuadict = {i:random.randint(10,100) for i in range(10)}\n",
    "print(Xinhuadict)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## $\\color{purple}{总结:}$\n",
    ">1.列表属于可变序列，他的元素可以随时修改或者删除，而元组为不可变序列，元组的元素是不能修改的\n",
    "2.列表可以使用append()、extend()、insert()、remove()和pop()等方法实现添加或者修改元素而元组没有几个方法可用因为他是不可变的\n",
    "3.列表支持切片访问和修改元素而元组也支持切片但不能修改元素\n",
    "4.元组比列表的访问和处理速度快，所以如果只需要访问元素的话优先选着元组\n",
    "5.列表、字典不能作为字典中的键而元组可以"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
